using System;
using System.IO;
using System.Windows;
using System.Collections.Generic;
using Qplus.beans;
using Qplus.Infrastructure;
using System.Diagnostics;

namespace Qplus.Commands
{

    /**
     * 
     *
     */
    public class GetRoomListCommand : AbsHeaderHttpCommand , HttpInputStreamProcessor{

	    const string COMMAND = "GetRoomList";

        public event EventHandler<MessageReceivedFromServer> GetRoomListCommandReceivedFromHttpServer;

	    /**
	     * @param command
	     * @param uid
	     * @param UKEY
	     */
	    public GetRoomListCommand(String uid, String UKEY)
            : base(COMMAND, uid, UKEY)
        {
		   
	    }

	    public void set(int pageNum){
		    setPageNum(pageNum);
	    }
	
	    private void setPageNum(int pageNum){
		    addParams("PageNumber", pageNum);
	    }

	    
	    public override Boolean processInputStream(Stream responseStream) {
		   //TODO
            return true;
	    }

	    
	    public override void onSuccess(int code) {
	    }

	    
	    public override void onFailed(int code) {
	    }
	
	    public void onGetRoomList(List<Room> list){
	    }
	
	  //  public HttpTask execute(string context, String url) {
		//    return base.execute(url, this);
        //  }
        public HttpTask execute(String url)
        {
            base.MessageReceivedFromHttpServer += new EventHandler<MessageReceivedFromServer>(GetRoomListCommand_MessageReceivedFromHttpServer);
            return base.execute(url, this);
        }

        void GetRoomListCommand_MessageReceivedFromHttpServer(object sender, MessageReceivedFromServer e)
        {
            //throw new NotImplementedException();
            string data = System.Text.UTF8Encoding.UTF8.GetString(e.Message, 0, e.Message.Length);
            Debug.WriteLine(data);
            ProcessIncomingMessage(data);
        }
        private void ProcessIncomingMessage(string ReceivedMessage)
        {
            // Future hook for handling the message in somewhere else.
            // It's most probably wise to put the parsing logic in some other class.
            if (GetRoomListCommandReceivedFromHttpServer != null)
                GetRoomListCommandReceivedFromHttpServer(this, new MessageReceivedFromServer(ReceivedMessage));
        }
    }
}
